package com.wsc.wsckeeper.activities;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;

import com.wsc.wsckeeper.R;
import com.wsc.wsckeeper.activities.base.BaseActivity;
import com.wsc.wsckeeper.adapter.BackupAdapter;
import com.wsc.wsckeeper.app.KFrontManager;
import com.wsc.wsckeeper.managers.FileServiceManager;
import com.wsc.wsckeeper.managers.PreferenceManager;
import com.wsc.wsckeeper.managers.ResourceManager;
import com.wsc.wsckeeper.utils.DialogUtils;

public class ImportActivity extends BaseActivity {

	private ListView list;
	private List<File> backups;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_import);
		super.onCreate(savedInstanceState);
	}

	@Override
	public void initComponents() {
		list = (ListView) findViewById(R.id.list);
	}

	@Override
	protected void onResume() {
		super.onResume();
		list.invalidateViews();
	}

	@Override
	public void initValues() {
		getActionBar().setSubtitle(R.string.action_import);

		File f = new File(PreferenceManager.getInstance().getLocation());
		File[] files = f.listFiles();
		if (files == null) {
			files = f.listFiles();
		}

		backups = new ArrayList<File>();

		for (File file : files) {
			if (!file.isDirectory() && file.canRead()) {
				if (file.getName().endsWith(FileServiceManager.FILE_TYPE)) {
					backups.add(file);
				}
			}
		}

		Collections.sort(backups, new Comparator<File>() {
			public int compare(File f1, File f2) {
				long i1 = f1.lastModified();
				long i2 = f2.lastModified();

				return (i1 > i2 ? -1 : (i1 == i2 ? 0 : 1));
			}
		});

		list.setAdapter(new BackupAdapter(this, backups));
	}

	@Override
	public void initListeners() {
		list.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
				final File file = backups.get(position);

				String message = ResourceManager.getStringValue(R.string.dialog_confirn_message);

				int index = file.getName().indexOf("_");
				String name = file.getName().substring(0, index);

				DialogUtils.confirmActionDialog(ImportActivity.this, message.replace("XXX", name), new Runnable() {

					@Override
					public void run() {
						try {
							FileServiceManager.getInstance().restoreDatabase(file.getName());
							KFrontManager.getInstance().cleanData();

							Intent result = new Intent();
							setResult(Activity.RESULT_OK, result);
							finish();

						} catch (IOException e) {
							showExceptionAsDialog(e);
						}
					}
				});
				return false;
			}
		});
	}

	@Override
	public void onBackPressed() {
		setResult(RESULT_CANCELED);
		super.onBackPressed();
	}
}
